package com.fudan.gamestore.service.impl;

import com.fudan.gamestore.mapper.GameMapper;
import com.fudan.gamestore.pojo.Game;
import com.fudan.gamestore.service.GameService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class GameServiceImpl implements GameService {

    @Autowired
    private GameMapper gameMapper;

    @Override
    public List<Game> getAllGame() {
        return gameMapper.getAllGame();
    }

    @Override
    public List<Game> getOwnGameByUserId(Integer userId) {
        List<Integer> gameIdList = gameMapper.getOwnGameIdByUserId(userId);
        if (gameIdList.size() == 0) return null;
        return gameMapper.getGameByIdList(gameIdList);
    }

    @Override
    public Integer addOwnGame(Integer userId, List<Integer> gameIdList) {
        return gameMapper.addOwnGame(userId, gameIdList);
    }

    @Override
    public Integer addGameSaleCount(List<Integer> gameIdList) {
        return gameMapper.addGameSaleCount(gameIdList);
    }

    @Override
    public PageInfo<Game> getAllGameByPage(Integer size, Integer pageNum) {
        PageHelper.startPage(pageNum, size);
        List<Game> gameList = gameMapper.getAllGame();
        return new PageInfo<>(gameList, 5);
    }

    @Override
    public PageInfo<Game> getOwnGameByUserIdPage(Integer userId, Integer size, Integer pageNum) {
        PageHelper.startPage(pageNum, size);
        List<Integer> gameIdList = gameMapper.getOwnGameIdByUserId(userId);
        if (gameIdList.size() == 0) return new PageInfo<>(new ArrayList<>(), 5);;
        List<Game> gameList = gameMapper.getGameByIdList(gameIdList);
        return new PageInfo<>(gameList, 5);
    }

    @Override
    public Game getGameById(Integer gameId) {
        return gameMapper.getGameById(gameId);
    }

    @Override
    public Integer addGame(Game game) {
        return gameMapper.insertGame(game);
    }

    @Override
    public Integer deleteGame(Integer gameId) {
        return gameMapper.deleteGame(gameId);
    }

    @Override
    public Integer modifyGame(Game game) {
        return gameMapper.updateGame(game);
    }
}
